package com.ourteam.workspace.dao.impl;

import com.ourteam.workspace.dao.IWorkspaceMemberDAO;
import com.ourteam.workspace.dao.WorkspaceMember;
import com.ourteam.workspace.dao.WorkspaceMemberDAOQueryBean;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import net.dao.AbstractSpringDAO;
import net.dao.DataAccessObjectHelper;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.RowMapper;

/**
 * Template Name : DAO服务对象 Template Version : 1 Template Last Modfiy Date : Fri Nov 17 11:00:11 CST
 * 2017 Auto Generate Date : 2018-11-30 18:42:34 Warning ! This file will be overwrite when
 * regenerate !
 */
public class AbstractWorkspaceMemberDAOJdbcImpl extends AbstractSpringDAO
    implements IWorkspaceMemberDAO {

  public static final String[] ENTITY_FIELDS =
      new String[] {
        Id,
        SysUserId,
        ScmAccount,
        Role,
        LastChangeCodeLine,
        Status,
        Remarks,
        LastActivityTime,
        WorkspaceId,
        UserName
      };

  public static final String[] ENTITY_PROPERTIES =
      new String[] {
        _DataObjectProperty.Id,
        _DataObjectProperty.SysUserId,
        _DataObjectProperty.ScmAccount,
        _DataObjectProperty.Role,
        _DataObjectProperty.LastChangeCodeLine,
        _DataObjectProperty.Status,
        _DataObjectProperty.Remarks,
        _DataObjectProperty.LastActivityTime,
        _DataObjectProperty.WorkspaceId,
        _DataObjectProperty.UserName
      };

  public static final String[] ENTITY_PK_FIELDS = new String[] {Id};

  public static final String[] ENTITY_AUTO_GEN_PK_FIELDS = new String[] {Id};

  public static final String[] ENTITY_UNIQUE_FIELDS = new String[] {};

  private static String relatedJoinSQL = null;

  private static String basicFilterSQL = null;

  private DataAccessObjectHelper<WorkspaceMember, WorkspaceMemberDAOQueryBean> daoHelper;

  public AbstractWorkspaceMemberDAOJdbcImpl() {
    super();
    daoHelper = new DataAccessObjectHelper<WorkspaceMember, WorkspaceMemberDAOQueryBean>(this);
  }

  protected String getEntityName() {
    return EntityName;
  }

  protected String[] getEntityFields() {
    return ENTITY_FIELDS;
  }

  protected String[] getEntityProperties() {
    return ENTITY_PROPERTIES;
  }

  protected String[] getEntityPKFields() {
    return ENTITY_PK_FIELDS;
  }

  protected String[] getEntityAutoGenPKFields() {
    return ENTITY_AUTO_GEN_PK_FIELDS;
  }

  protected String[] getEntityUniqueFields() {
    return ENTITY_UNIQUE_FIELDS;
  }

  protected String getBaiscQueryFilter() {

    if (basicFilterSQL == null) {
      StringBuffer basicFilterSQLbuffer = new StringBuffer();

      basicFilterSQL = basicFilterSQLbuffer.toString();
    }

    return basicFilterSQL;
  }

  protected String getRelatedJoinSQL() {

    if (relatedJoinSQL == null) {
      StringBuffer relatedJoinSQLBuffer = new StringBuffer();

      relatedJoinSQLBuffer
          .append(" LEFT JOIN ")
          .append(com.ourteam.system.dao.ISysUserDAO.EntityName)
          .append(" SYS_USER ");

      relatedJoinSQLBuffer.append(" ON ( ");

      relatedJoinSQLBuffer.append(
          StringUtils.replace(
              com.ourteam.system.dao.ISysUserDAO.Id,
              com.ourteam.system.dao.ISysUserDAO.EntityName + ".",
              "SYS_USER."));
      relatedJoinSQLBuffer.append(" = ").append(SysUserId);

      relatedJoinSQLBuffer.append(" ) ");

      relatedJoinSQL = relatedJoinSQLBuffer.toString();
    }

    return relatedJoinSQL;
  }

  /**
   * Check Data Unique
   *
   * @param aWorkspaceMember
   * @throws Exception
   */
  public boolean checkIsUnique(WorkspaceMember aWorkspaceMember) throws Exception {

    if (aWorkspaceMember == null) {
      return true;
    }

    List valueList = new ArrayList();

    List pkValueList = new ArrayList();

    pkValueList.add(aWorkspaceMember.getId());

    return isUnique(
        valueList.toArray(new Object[valueList.size()]),
        pkValueList.toArray(new Object[pkValueList.size()]));
  }

  /**
   * Insert Data
   *
   * @param aWorkspaceMember
   * @throws Exception
   */
  public void insert(WorkspaceMember aWorkspaceMember) throws Exception {

    super.doInsert(aWorkspaceMember);
  }

  /**
   * Batch Insert Datas
   *
   * @param aWorkspaceMember
   * @throws Exception
   */
  public void batchInsert(WorkspaceMember[] aWorkspaceMembers) throws Exception {

    super.doBatchInsert(aWorkspaceMembers);
  }

  /**
   * Delete Data
   *
   * @param final long id *
   * @throws Exception
   */
  public void delete(final long id) throws Exception {

    super.doDeleteById(new Object[] {new Long(id)});
  }

  /**
   * Delete Datas
   *
   * @param queryBean
   * @throws Exception
   */
  public void delete(WorkspaceMemberDAOQueryBean queryBean) throws Exception {

    super.doDeleteByQuery(queryBean);
  }

  public void delete(WorkspaceMember aWorkspaceMember) throws Exception {

    delete(aWorkspaceMember.getId());
  }

  /**
   * Update Data Selective
   *
   * @param aWorkspaceMember
   * @throws Exception
   */
  public void update(WorkspaceMember aWorkspaceMember) throws Exception {

    if (aWorkspaceMember.getId() == 0) {
      throw new Exception("Can not update object data whith out primary key value");
    }

    super.doUpdate(aWorkspaceMember);

    WorkspaceMember tempObj = findById(aWorkspaceMember.getId());

    if (tempObj != null) {
      PropertyUtils.copyProperties(aWorkspaceMember, tempObj);
    }
  }

  /**
   * Batch Update Datas
   *
   * @param aWorkspaceMembers[]
   * @throws Exception
   */
  public void batchUpdate(WorkspaceMember[] aWorkspaceMembers) throws Exception {

    super.doBatchUpdate(aWorkspaceMembers);
  }

  /**
   * Update Data By Condictions
   *
   * @param aWorkspaceMember
   * @param aValueCriteria
   * @param aTransaction
   * @throws Exception
   */
  public void update(WorkspaceMember aWorkspaceMember, WorkspaceMemberDAOQueryBean aQueryBean)
      throws Exception {

    super.doUpdateByQuery(aWorkspaceMember, aQueryBean);
  }

  /**
   * Update Data By Condictions
   *
   * @param aQueryBean
   * @throws Exception
   */
  public void update(WorkspaceMemberDAOQueryBean aQueryBean) throws Exception {
    super.doUpdateByQuery(aQueryBean);
  }

  protected void checkNullValue(WorkspaceMember aWorkspaceMember) throws Exception {

    if (aWorkspaceMember.getScmAccount() == null) {
      throw new Exception("The WorkspaceMember.scmAccount value is not allow NULL");
    }

    if (aWorkspaceMember.getRole() == null) {
      throw new Exception("The WorkspaceMember.role value is not allow NULL");
    }

    if (aWorkspaceMember.getLastActivityTime() == null) {
      throw new Exception("The WorkspaceMember.lastActivityTime value is not allow NULL");
    }

    if (aWorkspaceMember.getStatus() == null) {
      throw new Exception("The WorkspaceMember.status value is not allow NULL");
    }

    if (aWorkspaceMember.getRemarks() == null) {
      throw new Exception("The WorkspaceMember.remarks value is not allow NULL");
    }

    if (aWorkspaceMember.getUserName() == null) {
      throw new Exception("The WorkspaceMember.userName value is not allow NULL");
    }
  }

  /**
   * Get Data By ID
   *
   * @param final long id * @return ${businessObject.name}
   * @throws Exception
   */
  public WorkspaceMember findById(final long id) throws Exception {

    WorkspaceMemberDAOQueryBean queryBean = new WorkspaceMemberDAOQueryBean();

    WorkspaceMemberDAOQueryBean.Criteria criteria = queryBean.createCriteria();

    criteria.andIdEqualTo(id);

    // queryBean.setDistinct(true);

    WorkspaceMember[] WorkspaceMembers = query(queryBean);

    if (WorkspaceMembers.length == 0) {
      return null;
    } else {
      return WorkspaceMembers[0];
    }
  }

  /**
   * Query Data
   *
   * @param queryBean
   * @return WorkspaceMember[]
   * @throws Exception
   */
  public WorkspaceMember[] query(WorkspaceMemberDAOQueryBean queryBean) throws Exception {

    List result = super.doQuery(queryBean, new JdbcWorkspaceMemberRowMapper());

    return (WorkspaceMember[]) result.toArray(new WorkspaceMember[result.size()]);
  }

  /**
   * Query ${businessObject.name} Selective
   *
   * @param queryBean
   * @return Map[]
   * @model
   * @throws Exception
   */
  public Map<String, Object>[] querySelective(WorkspaceMemberDAOQueryBean queryBean)
      throws Exception {

    if (queryBean.getSelectProperties().isEmpty()) {
      queryBean.addAllSelectProperties();
    }

    return super.doQuerySelective(queryBean);
  }

  /**
   * Query Data Count
   *
   * @param queryBean
   * @return int
   * @throws Exception
   */
  public int queryCount(WorkspaceMemberDAOQueryBean queryBean) throws Exception {

    return super.doQueryCount(queryBean);
  }

  public void synchronize(
      WorkspaceMember[] aWorkspaceMembers, WorkspaceMemberDAOQueryBean queryBean) throws Exception {
    daoHelper.synchronize(aWorkspaceMembers, queryBean);
  }

  public void synchronize(
      WorkspaceMember[] aWorkspaceMembers,
      WorkspaceMemberDAOQueryBean queryBean,
      net.dao.IDataAccessInterface.ISynchronizeCallback<WorkspaceMember> callBack)
      throws Exception {
    daoHelper.synchronize(aWorkspaceMembers, queryBean, callBack);
  }

  public static class JdbcWorkspaceMemberRowMapper implements RowMapper<WorkspaceMember> {

    public WorkspaceMember mapRow(ResultSet resultSet, int arg1) throws SQLException {
      WorkspaceMember object = new WorkspaceMember();

      int columnIndex = 1;

      int __dataType;

      try {

        Object id = null;

        id = resultSet.getObject(columnIndex++);

        object.setIdNull(resultSet.getBoolean(columnIndex++));

        if (id != null) {

          setBeanValue(object, _DataObjectProperty.Id, id);
        }

        Object sysUserId = null;

        sysUserId = resultSet.getObject(columnIndex++);

        object.setSysUserIdNull(resultSet.getBoolean(columnIndex++));

        if (sysUserId != null) {

          setBeanValue(object, _DataObjectProperty.SysUserId, sysUserId);
        }

        Object scmAccount = null;

        scmAccount = resultSet.getObject(columnIndex++);

        object.setScmAccountNull(resultSet.getBoolean(columnIndex++));

        if (scmAccount != null) {

          setBeanValue(object, _DataObjectProperty.ScmAccount, scmAccount);
        }

        Object role = null;

        role = resultSet.getObject(columnIndex++);

        object.setRoleNull(resultSet.getBoolean(columnIndex++));

        if (role != null) {

          setBeanValue(object, _DataObjectProperty.Role, role);
        }

        Object lastChangeCodeLine = null;

        lastChangeCodeLine = resultSet.getObject(columnIndex++);

        object.setLastChangeCodeLineNull(resultSet.getBoolean(columnIndex++));

        if (lastChangeCodeLine != null) {

          setBeanValue(object, _DataObjectProperty.LastChangeCodeLine, lastChangeCodeLine);
        }

        Object status = null;

        status = resultSet.getObject(columnIndex++);

        object.setStatusNull(resultSet.getBoolean(columnIndex++));

        if (status != null) {

          setBeanValue(object, _DataObjectProperty.Status, status);
        }

        Object remarks = null;

        remarks = resultSet.getObject(columnIndex++);

        object.setRemarksNull(resultSet.getBoolean(columnIndex++));

        if (remarks != null) {

          setBeanValue(object, _DataObjectProperty.Remarks, remarks);
        }

        Object lastActivityTime = null;

        __dataType = resultSet.getMetaData().getColumnType(columnIndex);

        if (__dataType == java.sql.Types.DATE
            || __dataType == java.sql.Types.TIME
            || __dataType == java.sql.Types.TIMESTAMP) {
          lastActivityTime = resultSet.getTimestamp(columnIndex++);

        } else {
          lastActivityTime = resultSet.getObject(columnIndex++);
        }

        object.setLastActivityTimeNull(resultSet.getBoolean(columnIndex++));

        if (lastActivityTime != null) {

          setBeanValue(object, _DataObjectProperty.LastActivityTime, lastActivityTime);
        }

        Object workspaceId = null;

        workspaceId = resultSet.getObject(columnIndex++);

        object.setWorkspaceIdNull(resultSet.getBoolean(columnIndex++));

        if (workspaceId != null) {

          setBeanValue(object, _DataObjectProperty.WorkspaceId, workspaceId);
        }

        Object userName = null;

        userName = resultSet.getObject(columnIndex++);

        object.setUserNameNull(resultSet.getBoolean(columnIndex++));

        if (userName != null) {

          setBeanValue(object, _DataObjectProperty.UserName, userName);
        }

        return object;
      } catch (Exception e) {
        throw new SQLException(e.getMessage());
      }
    }
  }
}
